﻿using System;
using System.Web;
using System.Web.UI;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Owin;
using CorreoSocialWeb.Models;
using System.Drawing;
using System.Net.Mail;

namespace CorreoSocialWeb.Account
{
    public partial class ForgotPassword : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Forgot(object sender, EventArgs e)
        {
            try
            {
                FailureText.Text = String.Empty;
                lblErrorCorreo.Text = String.Empty;
                MailAddress m = new MailAddress(Email.Text);

                if (IsValid)
                {
                    // Validate the user password
                    var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                    ApplicationUser user = manager.FindByEmail(Email.Text);
                    if (user == null || !manager.IsEmailConfirmed(user.Id))
                    {
                        FailureText.Text = "El usuario no existe o no esta confirmado.";
                        FailureText.Visible = true;
                        return;
                    }
                    else
                    {
                        FailureText.Text = "Se envió un mail de recordatorio correctamente.";
                        FailureText.ForeColor = Color.Green;
                    }
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send email with the code and the redirect to reset password page
                    //string code = manager.GeneratePasswordResetToken(user.Id);
                    //string callbackUrl = IdentityHelper.GetResetPasswordRedirectUrl(code);
                    //manager.SendEmail(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>.");
                }
            }
            catch (FormatException)
            {
                lblErrorCorreo.ForeColor = System.Drawing.ColorTranslator.FromHtml("#b94a48");
                lblErrorCorreo.Text = "Formato de correo no válido. Debe ingresar correo@dominio.com.";
            }
            catch
            {
                FailureText.ForeColor = System.Drawing.ColorTranslator.FromHtml("#b94a48");
                FailureText.Text = "Ocurrió un error al enviar correo de confirmación. Intentelo mas tarde.";
            }
        }
    }
}